
// MultiMediaDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiMedia.h"
#include "MultiMediaDlg.h"
#include <commctrl.h>
#include <commdlg.h>
#include <stdio.h>
#include <strsafe.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CMultiMediaDlg dialog

CMultiMediaDlg * gInstance;
CMultiMediaDlg* CMultiMediaDlg::ShareInstance()
{
	return gInstance;
}

CMultiMediaDlg::CMultiMediaDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMultiMediaDlg::IDD, pParent)
	, m_mainDlg(0)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	gInstance = this;
}

void CMultiMediaDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CMultiMediaDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_FILE_OPENFILE, &CMultiMediaDlg::OnFileOpenfile)
	ON_WM_TIMER()
	ON_COMMAND(ID_FILE_EXIT, &CMultiMediaDlg::OnFileExit)
	ON_COMMAND(ID_CONTROL_PLAY, &CMultiMediaDlg::OnControlPlayPause)
	ON_COMMAND(ID_CONTROL_STOP, &CMultiMediaDlg::OnControlStop)
	ON_COMMAND(ID_CONTROL_NEXT, &CMultiMediaDlg::OnControlNext)
	ON_COMMAND(ID_CONTROL_PREVIOUS, &CMultiMediaDlg::OnControlPrevious)
	ON_COMMAND(ID_CONTROL_FULLSCREEN, &CMultiMediaDlg::OnControlFullscreen)
	ON_COMMAND(ID_CONTROL_MUTE, &CMultiMediaDlg::OnControlMute)
	ON_WM_WINDOWPOSCHANGED()
	ON_WM_GETMINMAXINFO()
END_MESSAGE_MAP()

//struct MediaStruct
//{
//	int ID;
//	CString FilePath;
//	CString FileName;
//};
//MediaStruct *myMediaList = new MediaStruct[1000];

// CMultiMediaDlg message handlers

BOOL CMultiMediaDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	m_Cmenu.LoadMenuW(IDR_MENU1);
	SetMenu(&m_Cmenu);

	CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);

	RECT dlgRect; 
	GetWindowRect(&dlgRect);
	m_rectParentDlg = GetCRectFromRECT(dlgRect);

	m_gControlMedia.Create(NULL, NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, NULL);
	m_gControlMedia.ShowWindow(SW_SHOW);
	m_gControlMedia.SetWindowPos(NULL,0,dlgRect.bottom-dlgRect.top-150, dlgRect.right, dlgRect.bottom, SWP_NOZORDER);

	m_gListFileMultiMedia.Create(NULL, NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, NULL);
	m_gListFileMultiMedia.ShowWindow(SW_SHOW);
	m_gListFileMultiMedia.SetWindowPos(NULL,0,dlgRect.top, dlgRect.left+200, dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);
 
	m_gShowVideo.Create(NULL, NULL, WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, CRect(0,0,0,0), this, NULL);
	m_gShowVideo.ShowWindow(SW_SHOW);

	m_gShowVideo.SetWindowPos(NULL,dlgRect.left+200,dlgRect.top, dlgRect.right-dlgRect.left - 200 , dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);
	
	nCount = 0;
	m_bIsPlaying = false;
	m_bIsStop = false;
	//m_nVol = -1;
	m_bAvoidReplayWhenAddFile = false;



	PostMessage(WM_GRAPHNOTIFY,NULL,NULL);	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMultiMediaDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CMultiMediaDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMultiMediaDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}
CRect CMultiMediaDlg::GetCRectFromRECT(RECT lpRECT)
{
	CRect rtRect;
	rtRect.left = lpRECT.left;
	rtRect.top = lpRECT.top;
	rtRect.right = lpRECT.right;// - lpRECT.left;
	rtRect.bottom = lpRECT.bottom;// - lpRECT.top;
	return rtRect;
}

Rect CMultiMediaDlg::GetRectFromRECT(RECT lpRECT)
{
	Rect rtRect;
	rtRect.X = lpRECT.left;
	rtRect.Y = lpRECT.top;
	rtRect.Width = lpRECT.right - lpRECT.left;
	rtRect.Height = lpRECT.bottom - lpRECT.top;
	return rtRect;
}


void CMultiMediaDlg::OnSize(UINT nType, int cx, int cy)
{
	CDialog::OnSize(nType, cx, cy);
	RECT dlgRect;
	GetClientRect(&dlgRect);
	if(m_gControlMedia.m_hWnd != NULL||m_gListFileMultiMedia.m_hWnd!=NULL|m_gShowVideo.m_hWnd!=NULL)
	{  

		m_gControlMedia.SetWindowPos(NULL,0,dlgRect.bottom-dlgRect.top-150, dlgRect.right, dlgRect.bottom, SWP_NOZORDER);   
		m_gListFileMultiMedia.SetWindowPos(NULL,dlgRect.left,dlgRect.top, dlgRect.left+200, dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);
		m_gShowVideo.SetWindowPos(NULL,dlgRect.left+200,dlgRect.top, dlgRect.right-dlgRect.left -200, dlgRect.bottom-dlgRect.top-150, SWP_NOREDRAW);
		//m_gSubTitle.SetWindowPos(NULL,0,dlgRect.bottom-dlgRect.top-250, dlgRect.right-200, dlgRect.bottom-dlgRect.top-150, SWP_NOZORDER);

	}
	if (m_VideoMng.m_state != 0)
		UpdateVideoScreen();


	
	
	//SendMessage(WM_GRAPHNOTIFY,NULL,NULL);
	
	// TODO: Add your message handler code here

}

BOOL CMultiMediaDlg::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
//	CRect rect;
//	GetClientRect(&rect);
////	CBrush myBrush(COLOR_VIEW_AREA);    // dialog background color
//	CBrush *pOld = pDC->SelectObject(&myBrush);
//	BOOL bRes  = pDC->PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY);
//	pDC->SelectObject(pOld);    // restore old brush

	return CDialog::OnEraseBkgnd(pDC);
}


void CMultiMediaDlg::OnFileOpenfile()
{
	// TODO: Add your command handler code here
	CString strFilter = _T("All files| *.mp3;*.wma;*.avi;*.wmv;*.flv;*.mkv;*.mov;*.mp4|");
	strFilter += _T("video file (*.avi;*.wmv;*.flv;*.mkv;*.mov;*.mp4)|*.avi;*.wmv;*.flv;*.mkv;*.mov;*.mp4|");
	strFilter += _T("audio file (*.mp3;*.wma)|*.mp3;*wma|");

	CFileDialog dlgFile(TRUE, NULL, NULL, OFN_FILEMUSTEXIST| OFN_HIDEREADONLY |OFN_ALLOWMULTISELECT,strFilter, NULL);
	dlgFile.m_ofn.lpstrTitle= _T("Open Media file");
	if (dlgFile.DoModal() == IDOK) 
	{
		

		POSITION pos; 
		pos = dlgFile.GetStartPosition(); 
		while(pos)
		{
			m_strFilePath=dlgFile.GetNextPathName(pos);
			int curPos= 0;
			m_strFileName= m_strFilePath.Tokenize(L"\\",curPos);
			while (m_strFileName != "")
			{	  
				m_strFileName= m_strFilePath.Tokenize(_T("\\"),curPos);
				if(m_strFileName.Find(L".avi") != -1 || m_strFileName.Find(L".flv") != -1 || m_strFileName.Find(L".mkv") != -1
					||m_strFileName.Find(L".mov") != -1 || m_strFileName.Find(L".mp4") != -1 || m_strFileName.Find(L".mpg") != -1
					||m_strFileName.Find(L".wmv") != -1 || m_strFileName.Find(L".mp3") != -1 || m_strFileName.Find(L".wma") != -1)
					break;
			}
			//----------------Add data to MediaPlayList----------------
			MediaObject si(nCount,m_strFilePath,m_strFileName);
			arrMediaObjects.Add(si);
			m_gListFileMultiMedia.AddDataToPlayList(this->m_gListFileMultiMedia.m_listControlFile,nCount,0,arrMediaObjects[nCount].GetFileName());
			nCount++;
		}
		if(!m_bAvoidReplayWhenAddFile)
		{
			MediaPlay();
			m_gListFileMultiMedia.PlayCurrentTrack();
			m_bAvoidReplayWhenAddFile = true;
		}
	}

}
void CMultiMediaDlg::MediaPlay()
{
	m_VideoMng.SetFilePath(arrMediaObjects[m_gListFileMultiMedia.m_nNumOfPlaylist].GetFilePath());
	m_gShowVideo.SetWndParentForVideoPlayerManager();
	m_VideoMng.SetRectPlayer(m_gShowVideo.m_rtPlayer);
	m_VideoMng.MultiMedia_Play();
	UpdateVideoScreen();
	MediaVolume(m_nVol);
	m_strTotalTime = m_VideoMng.GetTimeDuration();
	m_gControlMedia.m_stcTimeTotal.SetWindowTextW((LPCTSTR)m_strTotalTime);
	KillTimer(MY_TIMEREVENT);
	SetTimer(MY_TIMEREVENT, 1000, (TIMERPROC)NULL);
	m_bIsPlaying = true;
}


BOOL CMultiMediaDlg::PreTranslateMessage(MSG* pMsg)
{
	return CWnd::PreTranslateMessage(pMsg);
}
void CMultiMediaDlg::UpdateVideoScreen(void)
{
	RECT rc;
	m_gShowVideo.GetClientRect(&rc); 
	m_VideoMng.UpdateVideoWindow(&rc);
   
}

void CMultiMediaDlg::MediaStop()
{
	m_gControlMedia.m_stcTimePlaying.SetWindowTextW(L"00:00:00");
	m_gControlMedia.m_SliderSeeking.SetPos(MIN_RANGE_SEEK_SLIDER);
	m_VideoMng.MultiMedia_Stop();
	m_bIsPlaying = false;
	m_bIsStop = true;
}


int CMultiMediaDlg::DoTimeStuff()
{
	if(!m_bIsPlaying || m_VideoMng.m_lTimeElapsed >= m_VideoMng.m_lTotalDuration)
		return 0;
	m_gControlMedia.m_stcTimePlaying.SetWindowTextW(m_VideoMng.GetTimeElapsedOfMedia());
	m_gControlMedia.m_SliderSeeking.SetPos(m_VideoMng.m_nPos);
	if(m_VideoMng.m_nPos == MAX_RANGE_SEEK_SLIDER)
	{
		MediaStop();
	}
	else
		//m_btnPlay.EnableWindow(false);
	return 1;
}
void CMultiMediaDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: Add your message handler code here and/or call default
	switch(nIDEvent)
	{
	case MY_TIMEREVENT:
		DoTimeStuff();
		break;
	}
	CDialog::OnTimer(nIDEvent);
}

void CMultiMediaDlg::MediaVolume(int nVol)
{
	long lVol = 0;
	if(nVol > -8)
	{
		int val = 4000 / 8;
		lVol = nVol * val;
	}
	else
	{
		int val = 1000;
		lVol = nVol * val;
	}
	m_VideoMng.SetVolume(lVol);
}

void CMultiMediaDlg::NextOrPrevPlayList(bool bFlag)
{
	if(bFlag) //next
	{
		if(m_gListFileMultiMedia.m_nNumOfPlaylist < nCount-1)
		{
			m_gListFileMultiMedia.m_nOldNumNumOfPlaylist = m_gListFileMultiMedia.m_nNumOfPlaylist;
			m_gListFileMultiMedia.m_nNumOfPlaylist++;
		}
		else
		{
			m_gListFileMultiMedia.m_nOldNumNumOfPlaylist = m_gListFileMultiMedia.m_nNumOfPlaylist;
			m_gListFileMultiMedia.m_nNumOfPlaylist = 0;
		}
		MediaStop();
		MediaPlay();
		//m_gListFileMultiMedia.PlayCurrentTrack();
	}
	else //previous
	{
		if(m_gListFileMultiMedia.m_nNumOfPlaylist > 0)
		{
			m_gListFileMultiMedia.m_nOldNumNumOfPlaylist = m_gListFileMultiMedia.m_nNumOfPlaylist;
			m_gListFileMultiMedia.m_nNumOfPlaylist--;
		}
		else
		{
			m_gListFileMultiMedia.m_nOldNumNumOfPlaylist = m_gListFileMultiMedia.m_nNumOfPlaylist;
			m_gListFileMultiMedia.m_nNumOfPlaylist = nCount-1;
		}
		MediaStop();
		MediaPlay();
		//m_gListFileMultiMedia.PlayCurrentTrack();
	}

}
void CMultiMediaDlg::OnFileExit()
{
	// TODO: Add your command handler code here
	PostQuitMessage(0);

}

void CMultiMediaDlg::OnControlPlayPause()
{
	// TODO: Add your command handler code here
	m_gControlMedia.OnBnClickedPlayPause();
}

void CMultiMediaDlg::OnControlStop()
{
	// TODO: Add your command handler code here
	m_gControlMedia.OnBnClickedStop();
}

void CMultiMediaDlg::OnControlNext()
{
	// TODO: Add your command handler code here
	m_gControlMedia.OnBnClickedNext();
}

void CMultiMediaDlg::OnControlPrevious()
{
	// TODO: Add your command handler code here
	m_gControlMedia.OnBnClickedPrevious();
}

void CMultiMediaDlg::OnControlFullscreen()
{
	// TODO: Add your command handler code here
	m_gControlMedia.OnBnClickedFullScreen();
}

void CMultiMediaDlg::OnControlMute()
{
	// TODO: Add your command handler code here
}

void CMultiMediaDlg::OnWindowPosChanged(WINDOWPOS* lpwndpos)
{
	CDialog::OnWindowPosChanged(lpwndpos);


	//// TODO: Add your message handler code here
	//CRect rc; 
	//this->GetClientRect(rc); 
	////GetWindowRect(rc);
	//m_gShowVideo.m_Sub->SetWindowPos(&CWnd::wndTop, rc.left+225,rc.top  + 50 + rc.Height() * 0.85,rc.Width()-15,rc.Height()/4-50/*rc.left + 210, rc.top  + 50 + rc.Height() * 0.85, rc.Width(), rc.Height()/4*/, SWP_NOOWNERZORDER);
	//
}

void CMultiMediaDlg::OnGetMinMaxInfo(MINMAXINFO* lpMMI)
{
	// TODO: Add your message handler code here and/or call default
	// Preferred Minimum X & Y.
    const int MIN_SIZE_X = 1600;
    const int MIN_SIZE_Y = 900;
	
	// Set the Minimum Track Size. Used while resizing.
    lpMMI->ptMinTrackSize.x = MIN_SIZE_X;
    lpMMI->ptMinTrackSize.y = MIN_SIZE_Y;

	CDialog::OnGetMinMaxInfo(lpMMI);
}
